from exit import db
from datetime import datetime, date


from exit import db
from datetime import datetime, date


class MallBatch(db.Model):
    __tablename__ = 'd_mall_batch'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    order_id = db.Column(db.Integer, info='订单id')
    goods_id = db.Column(db.Integer)
    create_time = db.Column(db.DateTime, default=datetime.now, info='订单购买时间')
    user_id = db.Column(db.String(50))
    buy_type = db.Column(db.Integer, info='购买类型 1.原价购买 2摘牌购买 3特价购买 4优惠购买 5.转让')
    status = db.Column(db.Integer, default=0, info='0正常1冻结')
    deal_num = db.Column(db.Integer, default=0, info='成交数量')
    t_borrow_num = db.Column(db.Integer, default=0, info='原始资产一数量')
    t_release_num = db.Column(db.Integer, default=0, info='原始资产二数量')
    t_take_num = db.Column(db.Integer, default=0, info='原始提货数量')
    t_sell_num = db.Column(db.Integer, default=0, info='原始可卖数量')
    s_num_borrow = db.Column(db.Integer, default=0, info='预支票剩余')
    s_num_release = db.Column(db.Integer, default=0, info='释放票剩余')
    s_num_sell = db.Column(db.Integer, default=0, info='可卖票剩余')
    s_num_take = db.Column(db.Integer, default=0, info='提货票剩余')
    s_num_take2 = db.Column(db.Integer, default=0, info='强提货剩余')
    r_cycle = db.Column(db.Integer, info='释放周期')
    r_price = db.Column(db.DECIMAL(13, 2), info='下次在什么价格释放')
    r_num = db.Column(db.Integer, info='下次释放数量')
    r_price_list = db.Column(db.Text, info='释放价格以及数量列表')


class Cycle(db.Model):
    __tablename__ = 'd_mall_cycle'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    goods_id = db.Column(db.Integer)
    cycle = db.Column(db.Integer)
    start_time = db.Column(db.DateTime)
    end_time = db.Column(db.DateTime)
    status = db.Column(db.Integer)

    @staticmethod
    def insert_record(goods_id, cycle, start_time, end_time, status):
        record = Cycle(goods_id=goods_id, cycle=cycle, start_time=start_time, end_time=end_time, status=status)
        db.session.add(record)

    def cycle_json(self):
        return {
            'id': self.id,
            'goods_id': self.goods_id,
            'cycle': self.cycle,
            'start_time': self.start_time,
            'end_time': self.end_time,
            'status': self.status,
        }


class HangingOrder(db.Model):
    __tablename__ = 'd_mall_hanging_order'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.String(32))
    user_status = db.Column(db.Integer, info='1用户，2服务中心，3供应商')
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)
    status = db.Column(db.Integer, info='0挂牌中，1已成交，2手动撤销，3系统撤销')
    goods_id = db.Column(db.Integer)
    price = db.Column(db.DECIMAL(20, 2), info='单价')
    price_yj = db.Column(db.DECIMAL(20, 2), info='原价')
    goods_cycle = db.Column(db.Integer, info='商品周期')
    total_num = db.Column(db.Integer, info='数量')
    surplus_num = db.Column(db.Integer, info='剩余数量')
    r_type = db.Column(db.Integer, info='1服务中心单子，2供应商单子')

    @staticmethod
    def insert_record(user_id, user_status, status, goods_id, price, total_num, r_type):

        record = HangingOrder(user_id=user_id, user_status=user_status, status=status, goods_id=goods_id, price=price,
                              total_num=total_num, surplus_num=total_num, r_type=r_type)
        db.session.add(record)

    def hanging_order_json(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'user_status': self.user_status,
            'create_time': str(self.create_time),
            'update_time': str(self.update_time),
            'status': self.status,
            'goods_id': self.goods_id,
            'price': self.price,
            'total_num': self.total_num,
            'surplus_num': self.surplus_num,
            'r_type': self.r_type,
        }


class HangingOrderH(db.Model):
    __tablename__ = 'd_mall_hanging_order_h'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.String(32))
    user_status = db.Column(db.Integer, info='1用户，2服务中心，3供应商')
    create_time = db.Column(db.DateTime, default=datetime.now)
    status = db.Column(db.Integer, info='0挂牌中，1已成交，2手动撤销，3系统撤销')
    goods_id = db.Column(db.Integer)
    price = db.Column(db.DECIMAL(20, 2), info='单价')
    fees = db.Column(db.DECIMAL(20, 2), info='手续费')
    total_num = db.Column(db.Integer, info='数量')
    surplus_num = db.Column(db.Integer, info='剩余数量')
    sell_batch_id = db.Column(db.String(50), default='', info='卖家售出批次id')
    d_type = db.Column(db.Integer, info='1批发专区单子，2特价专区单子')

    @staticmethod
    def insert_record(user_id, user_status, status, goods_id, price, fees, total_num, sell_batch_id, d_type):

        record = HangingOrderH(user_id=user_id, user_status=user_status, status=status, goods_id=goods_id, price=price,
                               fees=fees, total_num=total_num, surplus_num=total_num, sell_batch_id=sell_batch_id,
                               d_type=d_type)
        db.session.add(record)

    def hanging_order_h_json(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'user_status': self.user_status,
            'create_time': self.create_time,
            'status': self.status,
            'goods_id': self.goods_id,
            'price': self.price,
            'fees': self.fees,
            'total_num': self.total_num,
            'surplus_num': self.surplus_num,
            'sell_batch_id': self.sell_batch_id,
            'd_type': self.d_type,
        }


class MallTrade(db.Model):
    __tablename__ = 'd_mall_trade'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_date = db.Column(db.Date, default=date.today)
    create_time = db.Column(db.DateTime, default=datetime.now)
    buy_order_id = db.Column(db.String(11), info='挂单买单id')
    transfer_order_id = db.Column(db.String(11), info='点对点订单')
    buyer_id = db.Column(db.String(50), info='买方')
    buyer_status = db.Column(db.Integer, info='0用户，1服务中心，2供应商')
    seller_id = db.Column(db.String(50), info='卖方')
    seller_status = db.Column(db.Integer, info='0用户，1服务中心，2供应商')
    deal_num = db.Column(db.Integer, info='成交数量')
    deal_price = db.Column(db.DECIMAL(10, 2), info='成交单价，竞拍价')
    deal_money = db.Column(db.DECIMAL(20, 2), info='成交总金额')
    goods_id = db.Column(db.Integer)
    buy_fee = db.Column(db.DECIMAL(20, 2), default=0.00)
    sell_fee = db.Column(db.DECIMAL(20, 2), default=0.00)
    goods_cycle = db.Column(db.Integer, info='成交周期')

    status = db.Column(db.Integer, default=0)
    deal_type = db.Column(db.Integer, info='成交方式 1.点对点 2.摘牌 3.商城 4.协议转让')
    buy_type = db.Column(db.Integer, info='购买类型 1.原价购买 2摘牌购买 3特价购买 4优惠购买 5.转让')
    sell_type = db.Column(db.Integer, info='卖出类型 1.可卖票卖出 2.预支票卖出')

    is_main = db.Column(db.Integer, default=1, info='是否是主要订单，因为预支点对点有带货订单')
    main_num = db.Column(db.Integer, info='主要数量')

    def trade_json(self):
        return {
            'id': self.id,
            'create_date': self.create_date,
            'create_time': self.create_time,
            'buy_order_id': self.buy_order_id,
            'buyer_id': self.buyer_id,
            'buyer_status': self.buyer_status,
            'seller_id': self.seller_id,
            'seller_status': self.seller_status,
            'deal_type': self.deal_type,
            'deal_num': self.deal_num,
            'deal_price': self.deal_price,
            'deal_money': self.deal_money,
            'goods_id': self.goods_id,
            'buy_fee': self.buy_fee,
            'sell_fee': self.sell_fee,
            'status': self.status,
            'goods_cycle': self.goods_cycle,
            'buy_type': self.buy_type,
            'sell_type': self.sell_type,
        }


class UserExtend2(db.Model):
    __tablename__ = 'c_user_extend2'
    id = db.Column(db.Integer, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    goods_id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.String(50), primary_key=True)
    s_coupon_num = db.Column(db.Integer, default=0, info='特价券数量')
    s_discount_num = db.Column(db.Integer, default=0, info='优惠券')
    s_borrow_num = db.Column(db.Integer, default=0, info='预支票')
    s_release_num = db.Column(db.Integer, default=0, info='释放票，资产二')
    s_sell_num = db.Column(db.Integer, default=0, info='可交易拍品')
    s_take_num = db.Column(db.Integer, default=0, info='提货，资产一')
    s_take_num2 = db.Column(db.Integer, default=0, info='只可提货，没有制度')
    user_status = db.Column(db.Integer, default=0, info='0正常用户，1服务中心，2供应商')

    @staticmethod
    def insert_record(goods_id, user_id):
        record = UserExtend2(goods_id, user_id, user_status=0, s_coupon_num=0, s_discount_num=0, s_borrow_num=0,
                             s_release_num=0, s_sell_num=0, s_take_num=0, s_take_num2=0)
        db.session.add(record)
        return record

    def user_extend2_json(self):
        return {
            'id': self.id,
            'goods_id': self.goods_id,
            'user_id': self.user_id,
            's_coupon_num': self.s_coupon_num,
            's_discount_num': self.s_discount_num,
            's_borrow_num': self.s_borrow_num,
            's_release_num': self.s_release_num,
            's_sell_num': self.s_sell_num,
            's_take_num': self.s_take_num,
            's_take_num2': self.s_take_num2,
            'user_status': self.user_status,
        }

    @staticmethod
    def return_user_extend2(user_id, goods_id):
        user_extend2 = UserExtend2.query.filter_by(user_id=user_id, goods_id=goods_id).with_for_update().first()
        if user_extend2 is None:
            record = UserExtend2(user_id=user_id, goods_id=goods_id)
            db.session.add(record)
            db.session.commit()
        return user_extend2


class MallCoupon(db.Model):
    __tablename__ = 'd_mall_s_coupon'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    order_id = db.Column(db.Integer)
    goods_id = db.Column(db.Integer)
    goods_cycle = db.Column(db.Integer, info='商品周期')
    create_time = db.Column(db.DateTime, default=datetime.now, info='创建日期，也是提货日期，7天后失效')
    expire_time = db.Column(db.DateTime, info='7天后失效')
    user_id = db.Column(db.String(50))
    num = db.Column(db.Integer, info='总数量')
    price = db.Column(db.DECIMAL(20, 2))
    s_num = db.Column(db.Integer, info='剩余数量')
    status = db.Column(db.Integer, default=0, info='0正常，1过期')
    t_type = db.Column(db.Integer, info='1提货2倍赠送，2过期产品返还')

    @staticmethod
    def insert_record(order_id, user_id, num, price, t_type):
        record = MallCoupon(order_id=order_id, user_id=user_id, num=num, price=price, t_type=t_type)
        db.session.add(record)

    def mall_coupon_json(self):
        return {
            "id": self.id,
            "order_id": self.order_id,
            "create_time": str(self.create_time),
            "expire_time": str(self.expire_time),
            "user_id": self.user_id,
            "num": self.num,
            "price": self.price,
            "s_num": self.s_num,
            "status": self.status,
            "t_type": self.t_type
        }


class MallTransferOrder(db.Model):
    __tablename__ = 'd_mall_transfer_order'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime)
    user_id = db.Column(db.String(100))         # 转方
    to_user_id = db.Column(db.String(100))      # 收方
    status = db.Column(db.Integer, default=0)   # 0待确认，1已确认，2用户手动撤单，3系统撤单
    num_ys = db.Column(db.DECIMAL(10, 2))       # 原始数量，因为有可能是带票的
    num = db.Column(db.Integer)                 # 真实数量
    price = db.Column(db.DECIMAL(10, 2))        # 单价
    total_money = db.Column(db.DECIMAL(20, 2))  # 总金额
    goods_id = db.Column(db.Integer)
    goods_cycle = db.Column(db.Integer)
    user_status = db.Column(db.Integer)
    deal_type = db.Column(db.Integer)           # 交易类型，1点对点交易
    sell_type = db.Column(db.Integer)           # 卖出方式，1可卖票卖出，2预支票卖出

    def transfer_order_json(self):
        return {
            'order_id': self.id,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else '',
            'deal_type': self.deal_type,
            'num': self.num,
            'price': self.price,
            'total_money': self.total_money,
            'status': self.status,
        }


